package com.xymtop.sqlcater.app.commend;


import com.xymtop.sqlcater.core.sql.sqlexecute.select.entity.*;
import com.xymtop.sqlcater.core.sql.sqlparser.toobj.parse.Parser;
import org.xmind.core.CoreException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//命令行形式来调用
public class CommendApp {


    //开启命令行APP
    public void run() throws IOException, CoreException, NoSuchFieldException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        System.out.println("1.加载测试的上下文对象");
        SqlSelect sqlSelect = new SqlSelect();

        SqlSelectField sqlSelectField = new SqlSelectField();
        sqlSelectField.setAsName("userid");
        SqlSelectField sqlSelectField1 = new SqlSelectField();
        sqlSelectField1.setName("id");

        List<SqlSelectField> sqlSelectFields = new ArrayList<>();
        sqlSelectFields.add(sqlSelectField);
        sqlSelectFields.add(sqlSelectField1);



        SelectCollect selectCollect = new SelectCollect();
        selectCollect.setName("user");
        selectCollect.setSelectCollectType(SelectCollectType.SQLTABLE);
        SelectCollect selectCollect1 = new SelectCollect();
        selectCollect1.setName("sys_user");
        selectCollect1.setSelectCollectType(SelectCollectType.SQLTABLE);


        List<SelectCollect> selectCollects = new ArrayList<>();
        selectCollects.add(selectCollect);
        selectCollects.add(selectCollect1);


        SqlJoin sqlJoin = new SqlJoin();
        sqlJoin.setIsMain(true);
        sqlJoin.setSelectCollect1(selectCollect);
        sqlJoin.setSelectCollect2(selectCollect1);

        List<SqlJoin> sqlJoins = new ArrayList<>();
        sqlJoins.add(sqlJoin);


        SqlWhere sqlWhere = new SqlWhere();
        Map<SqlSelectField,SqlSelectField> sqlSelectFieldSqlSelectFieldMap = new HashMap<>();
        sqlSelectFieldSqlSelectFieldMap.put(sqlSelectField,sqlSelectField1);
        sqlWhere.setWhereMap(sqlSelectFieldSqlSelectFieldMap);


        sqlJoin.setSqlWhere(sqlWhere);

        List<Group> groups = new ArrayList<>();
        Group group = new Group();
        group.setSelectFields(sqlSelectFields);
        groups.add(group);


        List<Having> havings = new ArrayList<>();
        Having having = new Having();
        having.setSqlSelectField1(sqlSelectField);
        having.setSqlSelectField2(sqlSelectField1);
        havings.add(having);


        List<OrderBy> orderByList = new ArrayList<>();
        OrderBy orderBy = new OrderBy();
        orderBy.setOrderByRule(OrderByRule.DESC);
        orderBy.setSqlSelectField(sqlSelectField);
        orderByList.add(orderBy);


//        设置对应的参数
        sqlSelect.setSqlSelectFields(sqlSelectFields);
        sqlSelect.setSelectCollects(selectCollects);
        sqlSelect.setOrderByList(orderByList);
        sqlSelect.setHavings(havings);
        sqlSelect.setGroups(groups);
        sqlSelect.setSqlWhere(sqlWhere);
        sqlSelect.setSqlJoins(sqlJoins);
//        System.out.println(sqlSelect.buildSql());


        SelectAppContent selectAppContent = new SelectAppContent();
        List<SqlSelect> list = new ArrayList<>();


        //在构建一个查询节点
        SqlSelect sqlSelect1 = new SqlSelect();
        sqlSelect1.setName("查人员");

        sqlSelect1.setSqlSelectFields(sqlSelectFields);
        sqlSelect1.setSelectCollects(selectCollects);
        sqlSelect1.setOrderByList(orderByList);
        sqlSelect1.setHavings(havings);
        sqlSelect1.setGroups(groups);
        sqlSelect1.setSqlWhere(sqlWhere);
        sqlSelect1.setSqlJoins(sqlJoins);

        list.add(sqlSelect);
        list.add(sqlSelect1);


        selectAppContent.setSelects(list);

        selectAppContent.out();


        //转换
        Parser parser = Parser.Parser(selectAppContent).parse();

        //需要去输出
        parser.out("./json.json");
        System.out.println("加载测试对象到:"+"./json.json文件，测试输出内容在run--build文件夹下面");
    }

}
